#include "mainscene.h"
#include "ui_mainscene.h"
#include <QPainter>
#include "mypushbutton.h"
#include "chooselevelscene.h"
#include <QTimer>
#include <QDebug>
#include <QSound>

MainScene::MainScene(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainScene)
{
    ui->setupUi(this);

    //设置大小
    setFixedSize(320,588);

    //设置图标
    setWindowIcon(QIcon(":/res/Coin0001.png"));

    //设置标题
    setWindowTitle("翻金币");

    QSound *startSound = new QSound(":/res/TapButtonSound.wav",this);

    //退出
    connect(ui->actionquit, &QAction::triggered,[=](){this->close();});

    //开始按钮
    MyPushButton *startBnt = new MyPushButton(":/res/MenuSceneStartButton.png");
    startBnt->setParent(this);
    startBnt->move( this->width() * 0.5 - startBnt->width() * 0.5 ,this->height() * 0.7 );

    //实例化选择关卡对象
    chooselScene = new ChooseLevelScene;

    //监听选择关卡的返回按钮的信号
    connect(chooselScene,&ChooseLevelScene::chooseSceneBack,this,[=](){
        chooselScene->hide(); //将选择关卡场景 隐藏掉
        this->setGeometry(chooselScene->geometry());
        this->show(); //重新显示主场景
    });


    connect(startBnt, &MyPushButton::clicked,[=](){
        qDebug()<<"startBnt"<<endl;
       startSound->play(); //开始音效
       startBnt->zoom1();
       startBnt->zoom2();

       //延时
       QTimer::singleShot(300, this, [=](){
           //进入关卡选择
           //隐藏自身
           this->hide();
           //显示关卡选择
           chooselScene->setGeometry(this->geometry());
           chooselScene->show();
       });
    });

}

void MainScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //画背景上图标
    pix.load(":/res/Title.png");

    pix = pix.scaled( pix.width() * 0.5 , pix.height() * 0.5);

    painter.drawPixmap(10,30,pix);
}

MainScene::~MainScene()
{
    delete ui;
}
